#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>


const char* ssid = "ESPap";
const char* password = "123456789";
WiFiServer server(80);


void setup() 
{
  delay(1000);
  Serial.begin(115200);
  delay(10);
  pinMode(D2, OUTPUT);
  digitalWrite(D2, LOW);
  Serial.print("Configuring access point...");

  WiFi.mode(WIFI_AP); 
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();
  Serial.println("HTTP server started");
}


void loop() 
{
  WiFiClient client = server.available();
  if (!client) 
  {
    return;
  }

  Serial.println("new client");
  while(!client.available())
  {
    Serial.print('.');
    delay(1);
  }
  
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  int val;
  if (req.indexOf("/led/0") != -1)      {val = 0;}
  else if (req.indexOf("/led/1") != -1)    {val = 1;}
  else 
  {
    Serial.println("None");
    client.stop();
    return;
  }
  digitalWrite(D2, val);
  client.flush();
  String s;
  if(val) {s="high";}
  else  {s="low";}

  client.print(s);
  delay(1);
  Serial.println("Client disonnected");
}